package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.entity.QuestionCommentInfo;
import com.example.computer_exam_training_sys.mapper.QuestionCommentMapper;
import com.example.computer_exam_training_sys.service.QuestionCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionCommentServiceImpl implements QuestionCommentService {
    @Autowired
    private QuestionCommentMapper commentMapper;

    @Override
    public int addComment(QuestionCommentInfo comment) {
        comment.setStatus(1); // 设置默认状态为正常 (1)
        return commentMapper.insertComment(comment);
    }

    @Override
    public int deleteComment(Integer comment_id) {
        return commentMapper.deleteComment(comment_id);
    }

    @Override
    public List<QuestionCommentInfo> getAllComments() {
        return commentMapper.getAllComments();
    }

    @Override
    public List<QuestionCommentInfo> getCommentsByQuestionId(Integer question_id) {
        return commentMapper.getCommentsByQuestionId(question_id);
    }

    @Override
    public Map<String, Object> getCommentsWithPagination(String search, int page, int size) {
        int offset = (page - 1) * size;
        List<QuestionCommentInfo> comments;
        int total;

        if (search != null && !search.isEmpty()) {
            comments = commentMapper.searchComments(search, offset, size);
            total = commentMapper.countSearchComments(search);
        } else {
            comments = commentMapper.getCommentsWithPagination(offset, size);
            total = commentMapper.countAllComments();
        }

        Map<String, Object> result = new HashMap<>();
        result.put("list", comments);
        result.put("total", total);
        return result;
    }

    @Override
    public int batchDeleteComments(List<Integer> commentIds) {
        if (commentIds == null || commentIds.isEmpty()) {
            return 0;
        }
        return commentMapper.batchDeleteComments(commentIds);
    }

    @Override
    public List<QuestionCommentInfo> getCommentsByTopicId(Integer topicId) {
        return commentMapper.getCommentsByTopicId(topicId);
    }

}